<?php

/**
 * Class to provide methods for handling customer companies
 *
 * @author Philipp Kiszka
 * @name project
 * @package Collabtive
 * @version 2.0
 * @link http://www.o-dyn.de
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
 */
class company {
	private $mylog;
	
	/**
	 * Constructor
	 * Initializes the event log
	 */
	function __construct() {
		$this->mylog = new mylog ();
	}
	
	/**
	 * Add a company
	 *
	 * @param array $data        	
	 * @return int $insid ID of the inserted company
	 */
	function add($data) {
		global $conn;
		
		$ins1Stmt = $conn->prepare ( "INSERT INTO company (`type`, `company`, `contact`, `email`, `phone`, `mobile`, `url`, `address`, `zip`, `city`, `country`, `state`, `desc`, `isDeleted`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)" );
		$ins1 = $ins1Stmt->execute ( array (
				$data ['type'],
				$data ['company'],
				$data ['contact'],
				$data ['email'],
				$data ['phone'],
				$data ['mobile'],
				$data ['url'],
				$data ['address'],
				$data ['zip'],
				$data ['city'],
				$data ['country'],
				$data ['state'],
				$data ['desc'],
				false 
		) );
		$insid = $conn->lastInsertId ();
		
		if ($ins1) {
			return $insid;
		} else {
			return false;
		}
	}
	
	/**
	 * Edit a company
	 *
	 * @param array $data
	 *        	Company data
	 * @return bool
	 */
	function edit($data) {
		global $conn;
		
		$id = ( int ) $data ['id'];
		
		$updStmt = $conn->prepare ( "UPDATE company SET `company`=?, `contact`=?, `email`=?, `phone`=?, `mobile`=?, `url`=?, `address`=?, `zip`=?, `city`=?, `country`=?, `state`=?, `desc`=? WHERE ID = ?" );
		$upd = $updStmt->execute ( array (
				$data ['company'],
				$data ['contact'],
				$data ['email'],
				$data ['phone'],
				$data ['mobile'],
				$data ['url'],
				$data ['address'],
				$data ['zip'],
				$data ['city'],
				$data ['country'],
				$data ['state'],
				$data ['desc'],
				$id 
		) );
		
		if ($upd) {
			return true;
		} else {
			return false;
		}
	}
	function updateCustomer($id, $email, $type, $phone, $cellphone, $company, $contactPerson, $url, $address, $city, $state, $country, $postcode, $description) {
		global $conn;
		$userid = $_SESSION ["userid"];
		$id = ( int ) $id;
		$type = ( int ) $type;
		$query = $conn->prepare ( "UPDATE `company` SET  `email`= '$email', `type`= '$type', `phone`= '$phone', 
				`mobile`= '$cellphone', `company`= '$company', `contact`= '$contactPerson', `url`= '$url', 
				`address`= '$address', `city`= '$city', `state`= '$state', `country`= '$country', `zip`= '$postcode',
				 `desc`= '$description' WHERE ID = $id" );
		$result = $query->execute ();
		
		if ($result) {
			$this->mylog->add ( $userid, 'updateCustomer', 3, $id );
			return true;
		} else {
			return false;
		}
	}
	function searchCustomers($keyword) {
		global $conn;
		
		if (strlen ( $keyword ) !== 0) {
			$request = "SELECT * FROM company JOIN type ON type.type_ID = company.type WHERE isDeleted != 1";
			$sel = $conn->query ( $request );
			$customers = array ();
			
			while ( $customer = $sel->fetch () ) {
				if ($this->is_str_contain ( strtolower ( $customer ["contact"] ), strtolower ( $keyword ) ) or $this->is_str_contain ( strtolower ( $customer ["email"] ), strtolower ( $keyword ) ) or $this->is_str_contain ( strtolower ( $customer ["phone"] ), strtolower ( $keyword ) ) or $this->is_str_contain ( strtolower ( $customer ["company"] ), strtolower ( $keyword ) ) or $this->is_str_contain ( strtolower ( $customer ["city"] ), strtolower ( $keyword ) ) or $this->is_str_contain ( strtolower ( $customer ["state"] ), strtolower ( $keyword ) ) or $this->is_str_contain ( strtolower ( $customer ["country"] ), strtolower ( $keyword ) ) or $this->is_str_contain ( strtolower ( $customer ["type_name"] ), strtolower ( $keyword ) )) {
					array_push ( $customers, $customer );
				}
			}
			if (! empty ( $customers )) {
				return $customers;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	/**
	 * Delete a company and disconnect all assigned projects
	 *
	 * @param int $id
	 *        	Company ID
	 * @return bool
	 */
	function del($id) {
		global $conn;
		$userid = $_SESSION ["userid"];
		
		$id = ( int ) $id;
		
		$query = $conn->prepare ( "DELETE FROM company_assigned WHERE customer = $id" );
		$result = $query->execute ();
		$query = $conn->query ( "UPDATE `company` SET `isDeleted`= 1 WHERE ID = $id" );
		$result = $query->execute ();
		if ($result) {
			$this->mylog->add ( $userid, 'delCustomer', 3, $id );
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Assign a company to a project
	 *
	 * @param int $task
	 *        	Company ID
	 * @param int $id
	 *        	project ID
	 * @return bool
	 */
	function assign($company, $id) {
		global $conn;
		
		$company = ( int ) $company;
		$id = ( int ) $id;
		
		$upd = $conn->query ( "INSERT INTO customers_assigned (customer, project) VALUES ($company, $id)" );
		
		if ($upd) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Disconnect a company from a user
	 *
	 * @param int $task
	 *        	Company ID
	 * @param int $id
	 *        	User ID
	 * @return bool
	 */
	function deassign($company, $id) {
		global $conn;
		
		$company = ( int ) $company;
		$id = ( int ) $id;
		
		$upd = $conn->query ( "DELETE FROM company_assigned WHERE user = $id AND company = $company" );
		
		if ($upd) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Get a company
	 *
	 * @param int $id
	 *        	Company ID
	 * @return array $company Company
	 */
	function getCompany($id) {
		global $conn;
		
		$id = ( int ) $id;
		
		$sel = $conn->prepare ( "SELECT * FROM company WHERE ID = ?" );
		$selStmt = $sel->execute ( array (
				$id 
		) );
		
		$company = $sel->fetch ();
		
		if (! empty ( $company )) {
			return $company;
		} else {
			return false;
		}
	}
	function getProjectCompany($project) {
		global $conn;
		
		$project = ( int ) $project;
		$sel = $conn->prepare ( "SELECT customer FROM customers_assigned WHERE project = ?" );
		$selStmt = $sel->execute ( array (
				$project 
		) );
		
		$companyId = $sel->fetch ();
		
		$company = $this->getCompany ( $companyId );
		
		if (! empty ( $company )) {
			return $company;
		} else {
			return false;
		}
	}
	/**
	 * Get a list of companies
	 *
	 * @param int $lim
	 *        	Maximum number of companies to return (default: 10)
	 * @return array $companies List of companies
	 */
	function getCompanies($lim = 10) {
		global $conn;
		
		$lim = ( int ) $lim;
		
		$sel = $conn->prepare ( "SELECT * FROM company WHERE isDeleted != 1 ORDER BY `company` ASC LIMIT $lim" );
		$selStmt = $sel->execute ();
		
		$companies = $sel->fetchAll ();
		
		if (! empty ( $companies )) {
			return $companies;
		} else {
			return false;
		}
	}
	
	/**
	 * Get a list of all companies
	 *
	 * @return array $companies List of all companies
	 */
	function getAllCompanies() {
		global $conn;
		
		$sel = $conn->query ( "SELECT * FROM company JOIN type ON type.type_ID = company.type WHERE isDeleted != 1" );
		$companies = array ();
		
		while ( $company = $sel->fetch () ) {
			array_push ( $companies, $company );
		}
		
		if (! empty ( $companies )) {
			return $companies;
		} else {
			return false;
		}
	}
	
	/**
	 * Get a company including all of its members
	 *
	 * @param int $id
	 *        	Company ID
	 * @return array $company Company including all of its members
	 */
	function getCompanyMembers($id) {
		global $conn;
		
		$id = ( int ) $id;
		
		$sel = $conn->query ( "SELECT user, company FROM company_assigned WHERE company = $id" );
		
		$staff = array ();
		$userobj = ( object ) new user ();
		$company = $this->getProfile ( $member [1] );
		
		while ( $member = $sel->fetch () ) {
			$user = $userobj->getProfile ( $member [0] );
			array_push ( $staff, $user );
		}
		
		$company ["staff"] = $staff;
		
		if (! empty ( $company )) {
			return $company;
		} else {
			return false;
		}
	}
	function is_str_contain($string, $keyword) {
		if (empty ( $string ) || empty ( $keyword ))
			return false;
		$keyword_first_char = $keyword [0];
		$keyword_length = strlen ( $keyword );
		$string_length = strlen ( $string );
		
		// case 1
		if ($string_length < $keyword_length)
			return false;
			
			// case 2
		if ($string_length == $keyword_length) {
			if ($string == $keyword)
				return true;
			else
				return false;
		}
		
		// case 3
		if ($keyword_length == 1) {
			for($i = 0; $i < $string_length; $i ++) {
				
				// Check if keyword's first char == string's first char
				if ($keyword_first_char == $string [$i]) {
					return true;
				}
			}
		}
		
		// case 4
		if ($keyword_length > 1) {
			for($i = 0; $i < $string_length; $i ++) {
				/*
				 * the remaining part of the string is equal or greater than the keyword
				 */
				if (($string_length + 1 - $i) >= $keyword_length) {
					
					// Check if keyword's first char == string's first char
					if ($keyword_first_char == $string [$i]) {
						$match = 1;
						for($j = 1; $j < $keyword_length; $j ++) {
							if (($i + $j < $string_length) && $keyword [$j] == $string [$i + $j]) {
								$match ++;
							} else {
								return false;
							}
						}
						
						if ($match == $keyword_length) {
							return true;
						}
						
						// end if first match found
					}
					
					// end if remaining part
				} else {
					return false;
				}
				
				// end for loop
			}
			
			// end case4
		}
		
		return false;
	}
}

?>
